package com.example.testdemo.zhihu_adv;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.testdemo.R;

import java.util.ArrayList;
import java.util.List;

public class ZhiHuAdvActivity extends AppCompatActivity {
    private RecyclerView recyclerView;

    private List<String> mDatas = new ArrayList<>();
    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhihu_adv_view);
        recyclerView = findViewById(R.id.rl_view);


        for (int i = 0; i< 50; i++){
            mDatas.add("");
        }
        final LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(new RecycleAdapter());

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            recyclerView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
                @Override
                public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                    int fPos = manager.findFirstVisibleItemPosition();
                    int lPos = manager.findLastCompletelyVisibleItemPosition();
                    for (int i = fPos; i <= lPos; i++) {
                        View view = manager.findViewByPosition(i);
                        AdImageViewVersion1 adImageView = view.findViewById(R.id.iv_adv);
                        if (adImageView.getVisibility() == View.VISIBLE) {
                            adImageView.setDy(manager.getHeight() - view.getTop());
                        }
                    }
                }
            });
        }
    }



    public class RecycleAdapter extends RecyclerView.Adapter<ZhiHuAdvActivity.RecycleAdapter.ViewHolder>{

        @Override
        public ZhiHuAdvActivity.RecycleAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(ZhiHuAdvActivity.this).inflate(R.layout.item_zhihu_adv_view, parent, false);
            ZhiHuAdvActivity.RecycleAdapter.ViewHolder holder = new ZhiHuAdvActivity.RecycleAdapter.ViewHolder(itemView);
            return holder;
        }

        @Override
        public void onBindViewHolder(ZhiHuAdvActivity.RecycleAdapter.ViewHolder holder, int position) {
            if(position == 5 || position == 20){
                holder.imageView.setVisibility(View.VISIBLE);
                holder.textView.setVisibility(View.GONE);
            }else{
                holder.imageView.setVisibility(View.GONE);
                holder.textView.setVisibility(View.VISIBLE);
            }

        }

        @Override
        public int getItemCount() {
            return mDatas.size();
        }
        class ViewHolder extends RecyclerView.ViewHolder{
            TextView textView;
            AdImageViewVersion1 imageView;
            public ViewHolder(View itemView) {
                super(itemView);
                textView = itemView.findViewById(R.id.threeTwo);
                imageView = itemView.findViewById(R.id.iv_adv);
            }
        }
    }

}
